<?php
require_once 'SortieStock.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BDDSortieStock
 *
 * @author Gael
 */
class BDDSortieStock  extends  SortieStock {
     static public function byId($id){
        try{
            $db= getPdo();
            try{ // on essai de lancer une transaction au cas ou elle serait déjà lancer dans une autre fonction
                $db->beginTransaction();
                $commit = true;
            }catch(PDOException $e){
                $commit = false;
                //echo'capture de lancement de transaction :'. $e->getMessage();
            }
            $rep = $db->query("select * from sortiestock where id=$id" );
            while($data =$rep->fetch()){
                $ta= new SortieStock($data['id'],  
                        $data['datesortie'],
                        BDDSociete::byId($data['idsociete']),
                        BDDEntrepot::byId($data['identrepot']),
                        BDDArticle::byId($data['idarticle']));
            }
            if($commit){ $db->commit();}
            $rep->closeCursor();
            return $ta;
        }catch(Exception $e){
            die('erreur requete'.$e->getMessage());
            return false;
        }
    }
    static public function all(){
        try{
            $db= getPdo();
            $tab = array();
            
            $rep = $db->query("select * from sortiestock  " );
            while($data =$rep->fetch()){
                
                $tab[$data['id']]=new SortieStock($data['id'],  
                        $data['datesortie'],
                        BDDSociete::byId($data['idsociete']),
                        BDDEntrepot::byId($data['identrepot']),
                        BDDArticle::byId($data['idarticle']));
            }
            $rep->closeCursor();
            return $tab;
        }catch(Exception $e){
            die('erreur requete'.$e->getMessage());
        }
    } 
     static function sortieArticle($idarticle,$identrepot,$idsociete){
        
        
        try{
            $db = getPdo();
            try{ // on essai de lancer une transaction au cas ou elle serait déjà lancer dans une autre fonction
                $db->beginTransaction();
                $commit = true;
            }catch(PDOException $e){
                $commit = false;
                //echo'capture de lancement de transaction :'. $e->getMessage();
            }
            $req =$db->prepare("INSERT INTO sortiestock(id, datesortie, idarticle,identrepot,idsociete) VALUES(:id, :date,:idarticle ,:identrepot,:idsociete)");
             if($req->execute(array('id' => NULL,'date' => date("Y-m-d"),'idarticle' => $idarticle,'identrepot' => $identrepot,'idsociete' => $idsociete))){
               
            }else{
                die(print_r($db->errorInfo(),true));
            }
           
          
            if($commit){ $db->commit();}
        }catch(Exception $e){
            $db->rollBack();
            die('erreur requete'.$e->getMessage());
            return false;
        }
       
        
    }
    
   
}

?>
